	//*****************************************************************************
//
//  File........: data_functions.c
//
//  Author(s)...: Miguel Velazquez
//
//  Target(s)...: ATmega169
//
//  Compiler....: AVR-GCC 4.1.1; avr-libc 1.4.5
//
//  Description.: Functions used to manage the flash memory
//
//  Date: 25/06/2013
// 
//  Revisions...: 711
//
//*****************************************************************************

#include <stdint.h>
#include "dataflash.h"
#include "data_functions.h"

uint8_t address_buffer1=255;//pointer to internal address of buffer to write
uint8_t address_buffer2=0;//pointer to internal address of buffer to read
uint16_t page=0;//page to read
uint32_t data_count=524287;//total of data (bytes) stored in flash (used as a pointer to last data)
uint32_t data_to_read=0;//always points to temperature data

uint16_t page_to_read=0;//page to read

char data[4];//array to save data {temperature,minute,hour,day}

char flash_state=NO_DATA;//determines if the flash is full or empty

void write_data(char value){
	while(!(Read_DF_status() & 0x80));
	if((data_count%4)==3) Page_To_Buffer(page,1);//if complete array is stored then save to page

	data_count++;
	get_page_and_address1(data_count);
	if(data_count>=524288) data_count=0;
	
	while(!(Read_DF_status() & 0x80));
	Buffer_Write_Byte (1, address_buffer1, value);
	if((data_count%4)==0) data_to_read=data_count;//set pointer "data_to_read" to last temperature value
	while(!(Read_DF_status() & 0x80));
	if(flash_state!=FULL) flash_state=NO_FULL;

}

void save_data_to_flash(void){
	Buffer_To_Page (1,page);//Buffer_To_Page (unsigned char BufferNo, unsigned int PageAdr)
}
/**************************************************************************************
void get_page_and_address1(uint32_t data)
this function gets the page and internal address where the data can be written
*************************************************************************************/
void get_page_and_address1(uint32_t data){
	page=(int) (data/256);
	address_buffer1=data-(page*256);
}
/**************************************************************************************
void get_page_and_address2(uint32_t data)
this function gets the page and internal address where the data can be read
*************************************************************************************/
void get_page_and_address2(uint32_t data){
	page_to_read=(int) (data/256);
	address_buffer2=data-(page_to_read*256);
}


/**************************************************************************************
void read_data(char x)
this function reads 4 bytes in the flash memory and stores them in data array,
if no data is stored then stores {0,0,0,0} in array
*************************************************************************************/
void read_data(char x){
//use buffer2 to read
if(flash_state==NO_DATA){
	data[0]=0;
	data[1]=0;
	data[2]=0;
	data[3]=0;
}

else{
	if(x==PREV_DATA){
		if(flash_state==NO_FULL){
			if(data_to_read==0) data_to_read=data_count-3;//points to temperature address
			else data_to_read=data_to_read-4;
		}
		//missing condition for flash_state==FULL
	}
	if(x==NEXT_DATA){
		if(flash_state==NO_FULL){
			if(data_to_read==data_count-3) data_to_read=0;
			else data_to_read=data_to_read+4;
		}
	}
	get_page_and_address2(data_to_read);
	while(!(Read_DF_status() & 0x80));
	Page_To_Buffer(page_to_read,1);//page_to_read
	while(!(Read_DF_status() & 0x80));
	data[0]=Buffer_Read_Byte (1, address_buffer2);//read temperature
	while(!(Read_DF_status() & 0x80));//Buffer_Read_Byte (unsigned char BufferNo, unsigned int IntPageAdr)
	data[1]=Buffer_Read_Byte (1, address_buffer2+1);//read minute
	while(!(Read_DF_status() & 0x80));
	data[2]=Buffer_Read_Byte (1, address_buffer2+2);//read hour
	while(!(Read_DF_status() & 0x80));
	data[3]=Buffer_Read_Byte (1, address_buffer2+3);//read day
}

}


